package com.zmm.member.controller;

import com.zmm.common.base.model.Result;
import com.zmm.common.pre.handle.Assert;
import com.zmm.member.model.Member;
import com.zmm.member.service.MemberService;
import com.zmm.member.vo.MemberVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Name MemberController
 * @Author Zebulon
 * @Date By 2021-06-21 21:56:23
 */
@Controller
@Slf4j
@RequestMapping("/member")
public class MemberController {

    @Resource
    private MemberService memberService;

    /**
    @Autowired
    public void setMemberService(MemberService memberService){
        this.memberService = memberService;
    }
    */

    /**
     * 控制器 - 用户管理 - 查询用户信息
     * @description:
     * @author: Zebulon
     * @date: 2021-06-21 21:59:37
     * @return: com.zmm.member.model.Member
     **/
    @ResponseBody
    @GetMapping("/find/member/info")
    public Result getMemberInfo(){
        List<Member> memberInfo = memberService.findMemberInfo();
        if (ObjectUtils.isEmpty(memberInfo)){
            log.error("查询的用户为空");
        }
        return Result.ok().setData(memberInfo);
    }

    /**
     * 控制器 - 用户管理 - go 注册页面
     * @description:
     * @author: Zebulon
     * @date: 2021-06-22 21:38:24
     * @return: java.lang.String
     **/
    @GetMapping("/to/register")
    public String toRegister(){
        return "member_register";
    }

    /**
     * 控制器 - 用户管理 - go 更新页面
     * @description:
     * @author: Zebulon
     * @date: 2021-06-22 21:38:58
     * @param id:
     * @param model: 
     * @return: java.lang.String
     **/
    @GetMapping("/to/update")
    public String toUpdateMember(Integer id, Model model){
        Assert.isPositiveInteger(id);
        Member member = memberService.findOneMemberById(id);
        model.addAttribute("member",member);
        return "member_update";
    }

    /**
     * 控制器 - 用户管理 - 处理用户注册
     * @description:
     * @author: Zebulon
     * @date: 2021-06-22 21:39:22
     * @param member: 
     * @return: com.zmm.common.base.model.Result
     **/
    @ResponseBody
    @PostMapping("/do/register")
    public Result doRegister(Member member){
        boolean save = memberService.addMember(member);
        return Result.ok();
    }

    /**
     * 控制器 - 用户管理 - 处理用户更新
     * @description:
     * @author: Zebulon
     * @date: 2021-06-22 21:39:38
     * @param member: 
     * @return: com.zmm.common.base.model.Result
     **/
    @ResponseBody
    @PostMapping("/do/update")
    public Result doUpdateMember(Member member){
        boolean update = memberService.modifyMember(member);
        return Result.ok();
    }

    /**
     * 根据 用户ID 查看用户详细信息
     * @description:
     * @author: Zebulon
     * @date: 2021-06-23 21:03:32
     * @param id:
     * @param model:
     * @return: java.lang.String
     **/
    @GetMapping("/find/by/id")
    public String findMemberById(Integer id, Model model){
        Assert.isPositiveInteger(id);
        Member member = memberService.findOneMemberById(id);
        model.addAttribute("member",member);
        return "member_view";
    }

    /**
     * 根据 vo 查询用户信息列表
     * @description:
     * @author: Zebulon
     * @date: 2021-06-23 21:05:26
     * @param memberVo:
     * @return: com.zmm.common.base.model.Result
     **/
    @GetMapping("/query/by/vo")
    public String queryMemberById(MemberVo memberVo,Model model){
        List<Member> memberList = memberService.queryMemberByVo(memberVo);
        //return Result.ok().setData(memberList)
        model.addAttribute("memberList",memberList);
        return "member_list";
    }

    @GetMapping("/delete/by/id")
    public String deleteMemberById(Integer id){
        memberService.deleteMemberById(id);
        return "member_list";
    }

}
